from django.shortcuts import render
from rest_framework import viewsets
from rest_framework.decorators import action  # 装饰器的模块
# Create your views here.
from rest_framework.response import Response
from rest_framework.status import HTTP_201_CREATED, HTTP_404_NOT_FOUND, HTTP_204_NO_CONTENT
from .models import Student, Classes
from .serializers import StudentSerializer, ClassesSerializer

# class StudentViewSet(viewsets.ViewSet):
#     def list(self, request):  # 查 多组
#         # 获取queryset对象
#         queryset = Student.objects.all()
#         # 序列化,实例化序列化器,将查询集转为了json格式数据
#         serializer = StudentSerializer(queryset, many=True)
#         # many=True 查询多条
#         return Response(serializer.data)
#
#     def create(self, request):
#         """
#         1.获取前端传进的数据
#         2.校验
#         3.保存数据
#         4.返回响应
#         :param request:
#         :return:
#         """
#         data = request.data
#         # 反序列化，转成模型对象
#         serializer = StudentSerializer(data=data)
#         serializer.is_valid(raise_exception=True)  # 遇到错误抛出异常
#         serializer.save()  # 没有错误就保存
#         return Response(serializer.data, status=HTTP_201_CREATED)
#
#     def retrieve(self, request, pk):
#         """
#         1.依据pk来查询指定对象，
#             如果有，则序列化
#             如果没有，返回404，找不到学生信息
#         2.返回响应
#         :param request:
#         :param pk:
#         :return:
#         """
#         try:
#             student = Student.objects.get(id=pk)  # 根据pk值来查询学生对象
#         except Student.DoesNotExist:
#             return Response(status=HTTP_404_NOT_FOUND)
#
#         serializer = StudentSerializer(instance=student)  # 指向try下面的student
#         return Response(serializer.data)
#
#     def update(self, request, pk):
#         """
#         1.依据pk来查询指定对象，
#             如果有，则序列化
#             如果没有，返回404，找不到学生信息
#         2.获取前端传进的数据
#         3.校验
#         4.保存数据
#         5.返回响应
#         :param request:
#         :param pk:
#         :return:
#         """
#         # 相当于打了一套组合拳
#         try:
#             student = Student.objects.get(id=pk)  # 根据pk值来查询学生对象
#         except Student.DoesNotExist:
#             return Response(status=HTTP_404_NOT_FOUND)
#
#         serializer = StudentSerializer(instance=student, data=request.data)
#         serializer.is_valid(raise_exception=True)  # 遇到错误抛出异常
#         serializer.save()  # 没有错误就保存
#         return Response(serializer.data)
#
#     def destroy(self, request, pk):
#         try:
#             student = Student.objects.get(id=pk)  # 根据pk值来查询学生对象
#         except Student.DoesNotExist:
#             return Response(status=HTTP_404_NOT_FOUND)
#
#         student.delete()
#         return Response(status=HTTP_204_NO_CONTENT)


class StudentViewSet(viewsets.ModelViewSet):
    queryset = Student.objects.filter(is_delete=False)  # 在前端只展示is_delete=False的学生
    serializer_class = StudentSerializer


class ClassesViewSet(viewsets.ModelViewSet):
    # list:
    # 查看全部班级信息
    # create:
    # 新增班级信息
    # retrieve:
    # 查看某个指定班级信息
    # update:
    # 修改班级信息
    # destroy:
    # 删除班级信息
    # last:
    # 查看最新班级信息
    queryset = Classes.objects.filter(is_delete=False)
    serializer_class = ClassesSerializer

    @action(methods=['get'], detail=False)  # detail=False不是详情,自然参数里也不要带pk
    def last(self, request):
        last_class = Classes.objects.filter(is_delete=False).last()  # 拿取到classes里最新的数据
        serializer = self.get_serializer(last_class)  # self.get_serializer就是调用了serializer_class
        return Response(serializer.data)

